<?php
require_once("../../config/config.inc.php");
require_once("../public/local.func.php");

$sEPName       = fn_getParam("strEPName","");
$sParentName   = fn_getParam("strParentName","");
$sManager      = fn_getParam("strManager","");
$sEPType       = fn_getParam("strEPType","");
$sCountryID    = fn_getParam("strCountryID","CN");
$sProvinceID   = fn_getParam("strProvinceID","0");
$sCityID       = fn_getParam("strCityID","0");
$sCountyID     = fn_getParam("strCountyID","0");
$sContacter    = fn_getParam("strContacter","");
$sAddress      = fn_getParam("strAddress","");
$sPostcode     = fn_getParam("strPostcode","");
$sTelephone    = fn_getParam("strTelephone","");
$sMobilephone  = fn_getParam("strMobilephone","");
$sFax          = fn_getParam("strFax","");
$sEMail        = fn_getParam("strEMail","");
$sWebsite      = fn_getParam("strWebsite","");
$sRemark       = fn_getParam("strRemark","");

$sAction = fn_getParam("strAction","");
$result  = "";
$sVIPID  = "";
$sEPID   = "";
if ($sAction == ACTION_ENTERPRISE) {
    $sql = "SELECT ID,VIPID FROM $TABLE_ENTERPRISE WHERE EPNAME='".$sEPName."'";
    $rset= $DB->query($sql);
    if ($row = mysql_fetch_array($rset)) {
        $sEPID  = $row["ID"];
        $sVIPID = $row["VIPID"];
        $result = "<font color='red'>该企事业单位 ".$sEPName." 已经存在，不能重复添加；</font>";
    }
    mysql_free_result($rset);
}
if ($sAction==ACTION_ENTERPRISE && $result=="") {
    //get max vip id in this month
    $sql = "SELECT YYYYMM_VIPID,MAX_VIPID FROM ".$TABLE_SYSTEM_MAXID;
    $sql.= " WHERE YYYYMM_VIPID='".date("Ym")."'";
//echo 'sql:'.$sql;
    $rset= $DB->query($sql);
    $row = mysql_fetch_array($rset);
    if (isset($row["YYYYMM_VIPID"]) && $row["YYYYMM_VIPID"]!='') {
        $sVIPID = $row["YYYYMM_VIPID"].str_pad($row["MAX_VIPID"],4,'0',STR_PAD_LEFT);
        $sql = "UPDATE ".$TABLE_SYSTEM_MAXID." SET MAX_VIPID=MAX_VIPID+1";
        $DB->query($sql);
    } else {
        $sVIPID = date("Ym").'0001';
        $sql = "UPDATE ".$TABLE_SYSTEM_MAXID." SET MAX_VIPID=2,YYYYMM_VIPID='".date("Ym")."'";
        $DB->query($sql);
    }
    unset($row);
    //

    $sql = "INSERT INTO $TABLE_ENTERPRISE(VIPID,EPNAME,PARENTNAME,MANAGER,EPTYPE,COUNTRY_ID,";
    $sql.= "PROVINCE_ID,CITY_ID,COUNTY_ID,CONTACTER,ADDRESS,POSTCODE,TELEPHONE,MOBILEPHONE,";
    $sql.= "FAX,EMAIL,WEBSITE,REGDATE,LOCKSTATUS,REMARK) ";
    $sql.= "VALUES('".$sVIPID."','".$sEPName."','".$sParentName."','".$sManager."','".$sEPType."','";
    $sql.= $sCountryID."',".$sProvinceID.",".$sCityID.",".$sCountyID.",'".$sContacter."','";
    $sql.= $sAddress."','".$sPostcode."','".$sTelephone."','".$sMobilephone."','";
    $sql.= $sFax."','".$sEMail."','".$sWebsite."',now(),'1','".$sRemark."')";
    $DB->query($sql);
    
    $sEPID = mysql_insert_id();
} else if ($result=="") {
    $sEPID = $DB->getFirstField("SELECT ID FROM ".$TABLE_ENTERPRISE." WHERE VIPID='".$USER_VIPID."'"); //common user
}

$SM->assign("sAction",$sAction);
$SM->assign("sEPID"  ,$sEPID);
$SM->assign("sVIPID" ,$sVIPID);
$SM->assign("result" ,$result);

$SM->assign("aCountryOption" ,getCountryArray());
$SM->assign("sCountrySelect" ,$sCountryID);
$SM->assign("aProvinceOption",getProvinceArray($sCountryID));

//get year,month,day list
$aYearOption = array();
$sYear = date("Y") - 5;
for ($i=0; $i<80; $i++) {
    $aYearOption[$sYear - $i] = $sYear - $i;
}
$aMonthOption = array();
for ($i=1; $i<13; $i++) {
    $aMonthOption[($i<10 ? "0".$i : $i)] = ($i<10 ? "0".$i : "".$i);
}
$aDayOption = array();
for ($i=1; $i<32; $i++) {
    $aDayOption[($i<10 ? "0".$i : $i)] = ($i<10 ? "0".$i : "".$i);
}
$SM->assign("aYearOption" ,$aYearOption);
$SM->assign("aMonthOption",$aMonthOption);
$SM->assign("aDayOption"  ,$aDayOption);

$SM->display("member/regUser.html");
?>
